package com.mrjy.integral.util;

import java.sql.Timestamp;

import com.mrjy.common.entity.IntegralInfo;
import com.mrjy.common.entity.UserInfo;
import com.mrjy.common.entity.UserMemberInfo;
import com.mrjy.common.util.SpringContextHelper;
import com.mrjy.integral.dao.IIntegralInfoDao;
import com.mrjy.maintain.dao.IUserInfoDao;
import com.mrjy.maintain.dao.IUserMemberInfoDao;

public class IntegralUtil {
	private static IUserMemberInfoDao userMemberInfoDao ;
	private static IIntegralInfoDao integralInfoDao;
	private static IUserInfoDao userInfoDao;
	/**
	 * 封装积分实体
	 * @param userId 用户id
	 * @param score 积分
	 * @param typeDesc 积分描述
	 * @param typeId 积分类型id
	 * @param infoId 积分记录id
	 * @return
	 */
	public static IntegralInfo setIntegral(Integer userId , int score
			,String typeDesc , String typeId , Integer infoId){
		
		if(userInfoDao == null){
			userInfoDao = (IUserInfoDao) SpringContextHelper.getBean(IUserInfoDao.class);
		}
		if(integralInfoDao == null ){
			integralInfoDao = (IIntegralInfoDao) SpringContextHelper.getBean(IIntegralInfoDao.class);
		}
		
		IntegralInfo info = new IntegralInfo();
		info.setUserId(userId);
		info.setCreateTime(new Timestamp(System.currentTimeMillis()));
		//购买会员，福卡，课程包，微课时，10倍积分
		if(typeId.equals(IntegralEnum.BUY_VIP.getTypeId()) || typeId.equals(IntegralEnum.BUY_COURSE.getTypeId())
		   || typeId.equals(IntegralEnum.BUY_VIDEO.getTypeId()) || typeId.equals(IntegralEnum.BUY_FUKA.getTypeId())){
			info.setScore(score);
		}else{
			if (userMemberInfoDao == null) {
	            userMemberInfoDao = (IUserMemberInfoDao) SpringContextHelper.getBean(IUserMemberInfoDao.class);
	        }
	        UserMemberInfo userMemberInfo = userMemberInfoDao.selectIsVipByUserId(userId);
	        if(null != userMemberInfo){//如果是会员，则积分乘以2
				if (score>0 && !typeId.equals(IntegralEnum.INTEGRAL_EXCHANGE.getTypeId())) {
					info.setScore(score*2);
				}else if (score <= 0) {
					info.setScore(score);
				}
			}else{
				info.setScore(score);
			}
		}
		
		info.setTypeDesc(typeDesc);
		info.setTypeId(typeId);
		info.setInfoId(infoId);
		info.setDisableFlag(0);
		
		integralInfoDao.insert(info) ;
		
		//会写userInfo表的总积分
		UserInfo ui = userInfoDao.queryById(userId.intValue()) ;
		if(ui == null){
			ui = new UserInfo() ;
			ui.setId(userId);
		}
		Integer integralB = ui.getIntegralBalance()==null?0:ui.getIntegralBalance() ;
		ui.setIntegralBalance(integralB + info.getScore());
		
		userInfoDao.updateById(ui);
		return info ;
	}
	/**
	 * 通过连续签到天数获取增加的积分
	 * @param day 连续签到天数
	 * @return
	 */
	public static int getScoreBySignInDay(int day){
		int score = 0 ;
		switch(day){
			case 1 :
				score = SignInEnum.FIRST.getScore();
				break ;
			case 2 :
				score = SignInEnum.SECOND.getScore();
				break;
			case 3 :
				score = SignInEnum.THIRD.getScore();
				break;
			case 4 :
				score = SignInEnum.FOURTH.getScore();
				break;
			case 5 : 
				score = SignInEnum.FIFTH.getScore();
				break;
            case 6:
                score = SignInEnum.SIXTH.getScore();
                break;
        }
		return score;
	}
}
